//#define DOTween

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if DOTween
using DG.Tweening;

#endif

namespace UFrame
{
    public static class UGUITool
    {
        /// <summary>
        /// 用于列表刷新,集合变化会继续增加
        /// 下标值从0开始
        /// </summary>
        public static void RefreshChild(int dataCount, Transform parent, Action<GameObject, int> refresh,
            GameObject row = null)
        {
            int originLen = parent.childCount;
            int count = 0;
            if (originLen > dataCount)
            {
                count = originLen;
            }
            else
            {
                count = dataCount;
            }

            GameObject go = null;

            if (row == null)
            {
                row = parent.GetChild(0).gameObject;
            }


            for (int i = 0; i < count; i++)
            {
                if (i >= originLen)
                {
                    go = GameObject.Instantiate(row);
                    go.transform.SetParent(parent);
                    go.transform.localScale = Vector3.one;
                }
                else
                {
                    go = parent.GetChild(i).gameObject;
                }

                if (i < dataCount)
                {
                    go.gameObject.SetActive(true);
                }
                else
                {
                    go.gameObject.SetActive(false);
                }

                if (refresh != null)
                {
                    refresh(go, i);
                }
            }
        }

        public static void RefreshProperty(List<GameObject> propertyList, int index, string attrName, float value,
            float nextValue, bool isMax)
        {
            Transform t = propertyList[index].transform;

            Text origin = t.Find("Origin").GetComponent<Text>();
            Text nextUp = t.Find("Up").GetComponent<Text>();

            Transform arrowUp = t.Find("ArrowImgUp");
            Transform arrowDown = t.Find("ArrowImgDown");
            arrowUp.gameObject.SetActive(false);
            arrowDown.gameObject.SetActive(false);


            if (nextValue >= value)
            {
                arrowUp.gameObject.SetActive(true);
            }
            else
            {
                arrowDown.gameObject.SetActive(true);
            }

            origin.text = attrName + (value * 100) + "%";
            nextUp.text = (nextValue * 100) + "%";

            if (isMax)
            {
                nextUp.text = "满级";
                arrowUp.gameObject.SetActive(false);
                arrowDown.gameObject.SetActive(false);
            }
        }


        public static void AddChild(GameObject parent, GameObject ui)
        {
            if (ui.transform.parent == parent)
            {
                return;
            }

            ui.transform.SetParent(parent.transform);
            ui.transform.localScale = Vector3.one;
            ui.transform.rotation = Quaternion.identity;
            ui.transform.position = Vector3.zero;
        }


        public static void DoCenterChild(ScrollRect scroll, int index)
        {
            int viewCount = 0;
            foreach (RectTransform t in scroll.content)
            {
                if (t.anchoredPosition3D.x < scroll.GetComponent<RectTransform>().rect.width)
                {
                    viewCount++;
                }
                else
                {
                    break;
                }
            }

            var rate = 1f / (scroll.content.childCount - viewCount);
            var center = Mathf.Ceil(viewCount / 2f) * rate;

#if DOTween
            //scroll.DOHorizontalNormalizedPos(Mathf.Clamp(rate * (index + 1) - center, 0, 1), 0.3f);
#endif
        }


#if DOTween
        public static Sequence DoAnimationNum(Text t, int mOldScore, int newScore, float timer = 0.4f)
        {
            Sequence sq = DOTween.Sequence();
            sq.SetAutoKill(false);

            sq.Append(DOTween.To(delegate(float value)
            {
                var temp = Math.Floor(value);        //向下取整
                t.text = temp + "";
            }, mOldScore, newScore, timer));
            return sq;
        }
#endif

        public static IEnumerator AnimationNum(Text t, int mOldScore, int newScore, float time = 0.4f)
        {
            float delta = (newScore - mOldScore) / time;
            float result = mOldScore;
            float frameDetail = 0.01f;

            do
            {
                result += delta * frameDetail;
                t.text = Math.Floor(result).ToString();
                yield return new WaitForSeconds(frameDetail);
            } while (result >= newScore);

            t.text = newScore.ToString();
        }

        public static Vector2 ConvertUIPos(Canvas canvas, RectTransform ui, Vector2 screenPos, Camera uiCamera)
        {
            Vector2 outPoint = Vector2.zero;
            if (canvas.renderMode == RenderMode.WorldSpace)
            {
                RectTransformUtility.ScreenPointToLocalPointInRectangle(ui, screenPos, uiCamera, out outPoint);
            }
            else if (canvas.renderMode == RenderMode.ScreenSpaceOverlay)
            {
                RectTransformUtility.ScreenPointToLocalPointInRectangle(ui, screenPos, null, out outPoint);
            }
            else if (canvas.renderMode == RenderMode.ScreenSpaceCamera)
            {
                RectTransformUtility.ScreenPointToLocalPointInRectangle(ui, screenPos, uiCamera, out outPoint);
            }

            return outPoint;
        }
    }
}